package lx8;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class RecriveMessage {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(11451);//接受时绑定端口

        //使用byte数组接受数据，类似中转站
        DatagramPacket dp = new DatagramPacket(new byte[1024*16],1024*16);

        //ds将数据存到dp中
        //该方法是阻塞的，会等待发送端发送消息
        ds.receive(dp);

        //解析数据

        System.out.println(dp.getAddress() + "  " + dp.getPort());
        System.out.println(dp.getOffset());
        System.out.println(dp.getSocketAddress() + "  " + dp.getClass());
        System.out.println(new String(dp.getData()));

        ds.close();
    }
}
